package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.utils.UploadUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")StrategyQueryObject qo, Model model){
        model.addAttribute("pageInfo",strategyService.query(qo));
        model.addAttribute("places",regionService.list());
        return "/strategy/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy,MultipartFile multipartFile){
        JSONResult result = new JSONResult();
        try{
            if(multipartFile!=null&&multipartFile.getSize()>0){
                String imagePath = UploadUtil.upload(multipartFile, UploadUtil.IMG_BASE_PATH + "/upload");
                strategy.setCoverUrl(imagePath);
            }
            strategyService.saveOrUpdate(strategy);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }




}
